import { mysqlTransaction } from "./mysqltransaction"

/**
 * 事务管理
 */
export class Transaction{

    /**
     * 事务嵌套层级
     */
    static transactionNestingLelvel : number

    /**
     * 是否只回滚
     */
    rollbackOnly :  boolean

    /**
     * 事务
     */
    tx : mysqlTransaction

    /**
     * 
     * @param conn 
     */
    constructor(conn : any){
        Transaction.transactionNestingLelvel = 0
        this.rollbackOnly = false
        this.tx = new mysqlTransaction(conn)
    }


    /**
     * 
     */
    async createSavepoint() {
        const savepointName = `SAVEPOINT_${Transaction.transactionNestingLelvel}`;
        await this.tx.createSavepoint(savepointName);
    }

    async releaseSavepoint() {
        const savepointName = `SAVEPOINT_${Transaction.transactionNestingLelvel}`;
        await this.tx.releaseSavepoint(savepointName);
    }

        /**
     * 回滚到保存点
     */
    async rollbackToSavepoint() {
        const savepointName = `SAVEPOINT_${Transaction.transactionNestingLelvel}`;
        await this.tx.rollbackToSavepoint(savepointName);
    }


        /**
     * 获取嵌套事务的保存点名称
     */
    static getNestTransactionSavePointName() {
        return `SAVEPOINT_${Transaction.transactionNestingLelvel}`;
    }
    
}